// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie;

import fmod.fmod.FMODSoundEmitter;
import fmod.fmod.FMOD_STUDIO_EVENT_CALLBACK;
import fmod.fmod.FMOD_STUDIO_EVENT_CALLBACK_TYPE;
import java.util.ArrayDeque;
import java.util.ArrayList;
import org.joml.Vector2f;
import zombie.Lua.LuaEventManager;
import zombie.audio.parameters.ParameterCameraZoom;
import zombie.audio.parameters.ParameterClosestWallDistance;
import zombie.audio.parameters.ParameterFogIntensity;
import zombie.audio.parameters.ParameterHardOfHearing;
import zombie.audio.parameters.ParameterInside;
import zombie.audio.parameters.ParameterMoodlePanic;
import zombie.audio.parameters.ParameterPowerSupply;
import zombie.audio.parameters.ParameterRainIntensity;
import zombie.audio.parameters.ParameterRoomSize;
import zombie.audio.parameters.ParameterRoomType;
import zombie.audio.parameters.ParameterSeason;
import zombie.audio.parameters.ParameterSnowIntensity;
import zombie.audio.parameters.ParameterStorm;
import zombie.audio.parameters.ParameterTemperature;
import zombie.audio.parameters.ParameterTimeOfDay;
import zombie.audio.parameters.ParameterWaterSupply;
import zombie.audio.parameters.ParameterWeatherEvent;
import zombie.audio.parameters.ParameterWindIntensity;
import zombie.audio.parameters.ParameterZone;
import zombie.audio.parameters.ParameterZoneWaterSide;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.core.math.PZMath;
import zombie.debug.DebugLog;
import zombie.debug.DebugType;
import zombie.input.Mouse;
import zombie.iso.Alarm;
import zombie.iso.BuildingDef;
import zombie.iso.IsoCamera;
import zombie.iso.IsoChunkMap;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoMetaCell;
import zombie.iso.IsoMetaGrid;
import zombie.iso.IsoObject;
import zombie.iso.IsoUtils;
import zombie.iso.IsoWorld;
import zombie.iso.RoomDef;
import zombie.iso.Vector2;
import zombie.iso.objects.RainManager;
import zombie.iso.weather.ClimateManager;
import zombie.network.GameClient;

public final class AmbientStreamManager extends zombie.BaseAmbientStreamManager {
    public static int OneInAmbienceChance = 2500;
    public static int MaxAmbientCount = 20;
    public static float MaxRange = 1000.0F;
    private final ArrayList<Alarm> alarmList = new ArrayList();
    public static zombie.BaseAmbientStreamManager instance;
    public final ArrayList<zombie.AmbientStreamManager.Ambient> ambient = new ArrayList();
    public final ArrayList<zombie.AmbientStreamManager.WorldSoundEmitter> worldEmitters = new ArrayList();
    public final ArrayDeque<zombie.AmbientStreamManager.WorldSoundEmitter> freeEmitters = new ArrayDeque();
    public final ArrayList<zombie.AmbientStreamManager.AmbientLoop> allAmbient = new ArrayList();
    public final ArrayList<zombie.AmbientStreamManager.AmbientLoop> nightAmbient = new ArrayList();
    public final ArrayList<zombie.AmbientStreamManager.AmbientLoop> dayAmbient = new ArrayList();
    public final ArrayList<zombie.AmbientStreamManager.AmbientLoop> rainAmbient = new ArrayList();
    public final ArrayList<zombie.AmbientStreamManager.AmbientLoop> indoorAmbient = new ArrayList();
    public final ArrayList<zombie.AmbientStreamManager.AmbientLoop> outdoorAmbient = new ArrayList();
    public final ArrayList<zombie.AmbientStreamManager.AmbientLoop> windAmbient = new ArrayList();
    public boolean initialized = false;
    private FMODSoundEmitter electricityShutOffEmitter = null;
    private long electricityShutOffEvent = 0L;
    private int electricityShutOffState = -1;
    private final ParameterFogIntensity parameterFogIntensity = new ParameterFogIntensity();
    private final ParameterRainIntensity parameterRainIntensity = new ParameterRainIntensity();
    private final ParameterSeason parameterSeason = new ParameterSeason();
    private final ParameterSnowIntensity parameterSnowIntensity = new ParameterSnowIntensity();
    private final ParameterStorm parameterStorm = new ParameterStorm();
    private final ParameterTimeOfDay parameterTimeOfDay = new ParameterTimeOfDay();
    private final ParameterTemperature parameterTemperature = new ParameterTemperature();
    private final ParameterWeatherEvent parameterWeatherEvent = new ParameterWeatherEvent();
    private final ParameterWindIntensity parameterWindIntensity = new ParameterWindIntensity();
    private final ParameterZone parameterZoneDeepForest = new ParameterZone("ZoneDeepForest", "DeepForest");
    private final ParameterZone parameterZoneFarm = new ParameterZone("ZoneFarm", "Farm");
    private final ParameterZone parameterZoneForest = new ParameterZone("ZoneForest", "Forest");
    private final ParameterZone parameterZoneNav = new ParameterZone("ZoneNav", "Nav");
    private final ParameterZone parameterZoneTown = new ParameterZone("ZoneTown", "TownZone");
    private final ParameterZone parameterZoneTrailerPark = new ParameterZone("ZoneTrailerPark", "TrailerPark");
    private final ParameterZone parameterZoneVegetation = new ParameterZone("ZoneVegetation", "Vegitation");
    private final ParameterZoneWaterSide parameterZoneWaterSide = new ParameterZoneWaterSide();
    private final ParameterCameraZoom parameterCameraZoom = new ParameterCameraZoom();
    private final ParameterClosestWallDistance parameterClosestWallDistance = new ParameterClosestWallDistance();
    private final ParameterHardOfHearing parameterHardOfHearing = new ParameterHardOfHearing();
    private final ParameterInside parameterInside = new ParameterInside();
    private final ParameterMoodlePanic parameterMoodlePanic = new ParameterMoodlePanic();
    private final ParameterPowerSupply parameterPowerSupply = new ParameterPowerSupply();
    private final ParameterRoomSize parameterRoomSize = new ParameterRoomSize();
    private final ParameterRoomType parameterRoomType = new ParameterRoomType();
    private final ParameterWaterSupply parameterWaterSupply = new ParameterWaterSupply();
    private final Vector2 tempo = new Vector2();
    private final FMOD_STUDIO_EVENT_CALLBACK electricityShutOffEventCallback = new FMOD_STUDIO_EVENT_CALLBACK() {
        public void timelineMarker(long var1, String string, int _int) {
            DebugLog.Sound.debugln("timelineMarker %s %d", string, _int);
            if ("ElectricityOff".equals(string)) {
                IsoWorld.instance.setHydroPowerOn(false);
                AmbientStreamManager.this.checkHaveElectricity();
            }
        }
    };

    public static zombie.BaseAmbientStreamManager getInstance() {
        return instance;
    }

    @Override
    public void update() {
        if (this.initialized) {
            if (!zombie.GameTime.isGamePaused()) {
                if (IsoPlayer.getInstance() != null) {
                    if (IsoPlayer.getInstance().getCurrentSquare() != null) {
                        this.updatePowerSupply();
                        this.parameterFogIntensity.update();
                        this.parameterRainIntensity.update();
                        this.parameterSeason.update();
                        this.parameterSnowIntensity.update();
                        this.parameterStorm.update();
                        this.parameterTemperature.update();
                        this.parameterTimeOfDay.update();
                        this.parameterWeatherEvent.update();
                        this.parameterWindIntensity.update();
                        this.parameterZoneDeepForest.update();
                        this.parameterZoneFarm.update();
                        this.parameterZoneForest.update();
                        this.parameterZoneNav.update();
                        this.parameterZoneVegetation.update();
                        this.parameterZoneTown.update();
                        this.parameterZoneTrailerPark.update();
                        this.parameterZoneWaterSide.update();
                        this.parameterCameraZoom.update();
                        this.parameterClosestWallDistance.update();
                        this.parameterHardOfHearing.update();
                        this.parameterInside.update();
                        this.parameterMoodlePanic.update();
                        this.parameterPowerSupply.update();
                        this.parameterRoomSize.update();
                        this.parameterRoomType.update();
                        this.parameterWaterSupply.update();
                        float float0 = zombie.GameTime.instance.getTimeOfDay();

                        for (int int0 = 0; int0 < this.worldEmitters.size(); int0++) {
                            zombie.AmbientStreamManager.WorldSoundEmitter worldSoundEmitter = (zombie.AmbientStreamManager.WorldSoundEmitter)this.worldEmitters
                                .get(int0);
                            if (worldSoundEmitter.daytime != null) {
                                IsoGridSquare isoGridSquare0 = IsoWorld.instance
                                    .CurrentCell
                                    .getGridSquare((double)worldSoundEmitter.x, (double)worldSoundEmitter.y, (double)worldSoundEmitter.z);
                                if (isoGridSquare0 == null) {
                                    worldSoundEmitter.fmodEmitter.stopAll();
                                    zombie.SoundManager.instance.unregisterEmitter(worldSoundEmitter.fmodEmitter);
                                    this.worldEmitters.remove(worldSoundEmitter);
                                    this.freeEmitters.add(worldSoundEmitter);
                                    int0--;
                                } else {
                                    if (float0 > worldSoundEmitter.dawn && float0 < worldSoundEmitter.dusk) {
                                        if (worldSoundEmitter.fmodEmitter.isEmpty()) {
                                            worldSoundEmitter.channel = worldSoundEmitter.fmodEmitter.playAmbientLoopedImpl(worldSoundEmitter.daytime);
                                        }
                                    } else if (!worldSoundEmitter.fmodEmitter.isEmpty()) {
                                        worldSoundEmitter.fmodEmitter.stopSound(worldSoundEmitter.channel);
                                        worldSoundEmitter.channel = 0L;
                                    }

                                    if (!worldSoundEmitter.fmodEmitter.isEmpty()
                                        && (IsoWorld.instance.emitterUpdate || worldSoundEmitter.fmodEmitter.hasSoundsToStart())) {
                                        worldSoundEmitter.fmodEmitter.tick();
                                    }
                                }
                            } else if (IsoPlayer.getInstance() != null && IsoPlayer.getInstance().Traits.Deaf.isSet()) {
                                worldSoundEmitter.fmodEmitter.stopAll();
                                zombie.SoundManager.instance.unregisterEmitter(worldSoundEmitter.fmodEmitter);
                                this.worldEmitters.remove(worldSoundEmitter);
                                this.freeEmitters.add(worldSoundEmitter);
                                int0--;
                            } else {
                                IsoGridSquare isoGridSquare1 = IsoWorld.instance
                                    .CurrentCell
                                    .getGridSquare((double)worldSoundEmitter.x, (double)worldSoundEmitter.y, (double)worldSoundEmitter.z);
                                if (isoGridSquare1 != null && !worldSoundEmitter.fmodEmitter.isEmpty()) {
                                    worldSoundEmitter.fmodEmitter.x = worldSoundEmitter.x;
                                    worldSoundEmitter.fmodEmitter.y = worldSoundEmitter.y;
                                    worldSoundEmitter.fmodEmitter.z = worldSoundEmitter.z;
                                    if (IsoWorld.instance.emitterUpdate || worldSoundEmitter.fmodEmitter.hasSoundsToStart()) {
                                        worldSoundEmitter.fmodEmitter.tick();
                                    }
                                } else {
                                    worldSoundEmitter.fmodEmitter.stopAll();
                                    zombie.SoundManager.instance.unregisterEmitter(worldSoundEmitter.fmodEmitter);
                                    this.worldEmitters.remove(worldSoundEmitter);
                                    this.freeEmitters.add(worldSoundEmitter);
                                    int0--;
                                }
                            }
                        }

                        float float1 = zombie.GameTime.instance.getNight();
                        boolean boolean0 = IsoPlayer.getInstance().getCurrentSquare().isInARoom();
                        boolean boolean1 = RainManager.isRaining();

                        for (int int1 = 0; int1 < this.allAmbient.size(); int1++) {
                            ((zombie.AmbientStreamManager.AmbientLoop)this.allAmbient.get(int1)).targVol = 1.0F;
                        }

                        for (int int2 = 0; int2 < this.nightAmbient.size(); int2++) {
                            ((zombie.AmbientStreamManager.AmbientLoop)this.nightAmbient.get(int2)).targVol *= float1;
                        }

                        for (int int3 = 0; int3 < this.dayAmbient.size(); int3++) {
                            ((zombie.AmbientStreamManager.AmbientLoop)this.dayAmbient.get(int3)).targVol *= 1.0F - float1;
                        }

                        for (int int4 = 0; int4 < this.indoorAmbient.size(); int4++) {
                            ((zombie.AmbientStreamManager.AmbientLoop)this.indoorAmbient.get(int4)).targVol *= boolean0 ? 0.8F : 0.0F;
                        }

                        for (int int5 = 0; int5 < this.outdoorAmbient.size(); int5++) {
                            ((zombie.AmbientStreamManager.AmbientLoop)this.outdoorAmbient.get(int5)).targVol *= boolean0 ? 0.15F : 0.8F;
                        }

                        for (int int6 = 0; int6 < this.rainAmbient.size(); int6++) {
                            ((zombie.AmbientStreamManager.AmbientLoop)this.rainAmbient.get(int6)).targVol *= boolean1 ? 1.0F : 0.0F;
                            if (((zombie.AmbientStreamManager.AmbientLoop)this.rainAmbient.get(int6)).channel != 0L) {
                                fmod.javafmod.FMOD_Studio_EventInstance_SetParameterByName(
                                    ((zombie.AmbientStreamManager.AmbientLoop)this.rainAmbient.get(int6)).channel,
                                    "RainIntensity",
                                    ClimateManager.getInstance().getPrecipitationIntensity()
                                );
                            }
                        }

                        for (int int7 = 0; int7 < this.allAmbient.size(); int7++) {
                            ((zombie.AmbientStreamManager.AmbientLoop)this.allAmbient.get(int7)).update();
                        }

                        for (int int8 = 0; int8 < this.alarmList.size(); int8++) {
                            ((Alarm)this.alarmList.get(int8)).update();
                            if (((Alarm)this.alarmList.get(int8)).finished) {
                                this.alarmList.remove(int8);
                                int8--;
                            }
                        }

                        this.doOneShotAmbients();
                    }
                }
            }
        }
    }

    @Override
    public void doOneShotAmbients() {
        for (int _int = 0; _int < this.ambient.size(); _int++) {
            zombie.AmbientStreamManager.Ambient ambientx = (zombie.AmbientStreamManager.Ambient)this.ambient.get(_int);
            if (ambientx.finished()) {
                DebugLog.log(DebugType.Sound, "ambient: removing ambient sound " + ambientx.name);
                this.ambient.remove(_int--);
            } else {
                ambientx.update();
            }
        }
    }

    @Override
    public void addRandomAmbient() {
        if (!Core.GameMode.equals("LastStand") && !Core.GameMode.equals("Tutorial")) {
            ArrayList arrayList = new ArrayList();

            for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
                IsoPlayer isoPlayer0 = IsoPlayer.players[_int];
                if (isoPlayer0 != null && isoPlayer0.isAlive()) {
                    arrayList.add(isoPlayer0);
                }
            }

            if (!arrayList.isEmpty()) {
                IsoPlayer isoPlayer1 = (IsoPlayer)arrayList.get(Rand.Next(arrayList.size()));
                String string = "";
                if (zombie.GameTime.instance.getHour() > 7 && zombie.GameTime.instance.getHour() < 21) {
                    switch (Rand.Next(3)) {
                        case 0:
                            if (Rand.Next(10) < 2) {
                                string = "MetaDogBark";
                            }
                            break;
                        case 1:
                            if (Rand.Next(10) < 3) {
                                string = "MetaScream";
                            }
                    }
                } else {
                    switch (Rand.Next(5)) {
                        case 0:
                            if (Rand.Next(10) < 2) {
                                string = "MetaDogBark";
                            }
                            break;
                        case 1:
                            if (Rand.Next(13) < 3) {
                                string = "MetaScream";
                            }
                            break;
                        case 2:
                            string = "MetaOwl";
                            break;
                        case 3:
                            string = "MetaWolfHowl";
                    }
                }

                if (!string.isEmpty()) {
                    float float0 = isoPlayer1.x;
                    float float1 = isoPlayer1.y;
                    double _double = (double)Rand.Next((float) -Math.PI, (float) Math.PI);
                    this.tempo.x = (float)Math.cos(_double);
                    this.tempo.y = (float)Math.sin(_double);
                    this.tempo.setLength(1000.0F);
                    float0 += this.tempo.x;
                    float1 += this.tempo.y;
                    if (!GameClient.bClient) {
                        System.out
                            .println("playing ambient: " + string + " at dist: " + Math.abs(float0 - isoPlayer1.x) + "," + Math.abs(float1 - isoPlayer1.y));
                        zombie.AmbientStreamManager.Ambient ambientx = new zombie.AmbientStreamManager.Ambient(
                            string, float0, float1, 50.0F, Rand.Next(0.2F, 0.5F)
                        );
                        this.ambient.add(ambientx);
                    }
                }
            }
        }
    }

    @Override
    public void addBlend(String name, float vol, boolean bIndoors, boolean bRain, boolean bNight, boolean bDay) {
        zombie.AmbientStreamManager.AmbientLoop ambientLoop = new zombie.AmbientStreamManager.AmbientLoop(0.0F, name, vol);
        this.allAmbient.add(ambientLoop);
        if (bIndoors) {
            this.indoorAmbient.add(ambientLoop);
        } else {
            this.outdoorAmbient.add(ambientLoop);
        }

        if (bRain) {
            this.rainAmbient.add(ambientLoop);
        }

        if (bNight) {
            this.nightAmbient.add(ambientLoop);
        }

        if (bDay) {
            this.dayAmbient.add(ambientLoop);
        }
    }

    @Override
    public void init() {
        if (!this.initialized) {
            this.initialized = true;
        }
    }

    @Override
    public void doGunEvent() {
        ArrayList arrayList = new ArrayList();

        for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
            IsoPlayer isoPlayer0 = IsoPlayer.players[_int];
            if (isoPlayer0 != null && isoPlayer0.isAlive()) {
                arrayList.add(isoPlayer0);
            }
        }

        if (!arrayList.isEmpty()) {
            IsoPlayer isoPlayer1 = (IsoPlayer)arrayList.get(Rand.Next(arrayList.size()));
            String string = null;
            switch (Rand.Next(6)) {
                case 0:
                    string = "MetaAssaultRifle1";
                    break;
                case 1:
                    string = "MetaPistol1";
                    break;
                case 2:
                    string = "MetaShotgun1";
                    break;
                case 3:
                    string = "MetaPistol2";
                    break;
                case 4:
                    string = "MetaPistol3";
                    break;
                case 5:
                    string = "MetaShotgun1";
            }

            float float0 = isoPlayer1.x;
            float float1 = isoPlayer1.y;
            short _short = 600;
            double _double = (double)Rand.Next((float) -Math.PI, (float) Math.PI);
            this.tempo.x = (float)Math.cos(_double);
            this.tempo.y = (float)Math.sin(_double);
            this.tempo.setLength((float)(_short - 100));
            float0 += this.tempo.x;
            float1 += this.tempo.y;
            zombie.WorldSoundManager.instance.addSound(null, (int)float0, (int)float1, 0, _short, _short);
            float float2 = 1.0F;
            zombie.AmbientStreamManager.Ambient ambientx = new zombie.AmbientStreamManager.Ambient(string, float0, float1, 700.0F, float2);
            this.ambient.add(ambientx);
        }
    }

    @Override
    public void doAlarm(RoomDef room) {
        if (room != null && room.building != null && room.building.bAlarmed) {
            room.building.bAlarmed = false;
            room.building.setAllExplored(true);
            this.alarmList.add(new Alarm(room.x + room.getW() / 2, room.y + room.getH() / 2));
        }
    }

    @Override
    public void stop() {
        for (zombie.AmbientStreamManager.AmbientLoop ambientLoop : this.allAmbient) {
            ambientLoop.stop();
        }

        this.allAmbient.clear();
        this.ambient.clear();
        this.dayAmbient.clear();
        this.indoorAmbient.clear();
        this.nightAmbient.clear();
        this.outdoorAmbient.clear();
        this.rainAmbient.clear();
        this.windAmbient.clear();
        this.alarmList.clear();
        if (this.electricityShutOffEmitter != null) {
            this.electricityShutOffEmitter.stopAll();
            this.electricityShutOffEvent = 0L;
        }

        this.electricityShutOffState = -1;
        this.initialized = false;
    }

    @Override
    public void addAmbient(String name, int x, int y, int radius, float volume) {
        if (GameClient.bClient) {
            zombie.AmbientStreamManager.Ambient ambientx = new zombie.AmbientStreamManager.Ambient(name, (float)x, (float)y, (float)radius, volume, true);
            this.ambient.add(ambientx);
        }
    }

    @Override
    public void addAmbientEmitter(float x, float y, int z, String name) {
        zombie.AmbientStreamManager.WorldSoundEmitter worldSoundEmitter = this.freeEmitters.isEmpty()
            ? new zombie.AmbientStreamManager.WorldSoundEmitter()
            : (zombie.AmbientStreamManager.WorldSoundEmitter)this.freeEmitters.pop();
        worldSoundEmitter.x = x;
        worldSoundEmitter.y = y;
        worldSoundEmitter.z = (float)z;
        worldSoundEmitter.daytime = null;
        if (worldSoundEmitter.fmodEmitter == null) {
            worldSoundEmitter.fmodEmitter = new FMODSoundEmitter();
        }

        worldSoundEmitter.fmodEmitter.x = x;
        worldSoundEmitter.fmodEmitter.y = y;
        worldSoundEmitter.fmodEmitter.z = (float)z;
        worldSoundEmitter.channel = worldSoundEmitter.fmodEmitter.playAmbientLoopedImpl(name);
        worldSoundEmitter.fmodEmitter.randomStart();
        zombie.SoundManager.instance.registerEmitter(worldSoundEmitter.fmodEmitter);
        this.worldEmitters.add(worldSoundEmitter);
    }

    @Override
    public void addDaytimeAmbientEmitter(float x, float y, int z, String name) {
        zombie.AmbientStreamManager.WorldSoundEmitter worldSoundEmitter = this.freeEmitters.isEmpty()
            ? new zombie.AmbientStreamManager.WorldSoundEmitter()
            : (zombie.AmbientStreamManager.WorldSoundEmitter)this.freeEmitters.pop();
        worldSoundEmitter.x = x;
        worldSoundEmitter.y = y;
        worldSoundEmitter.z = (float)z;
        if (worldSoundEmitter.fmodEmitter == null) {
            worldSoundEmitter.fmodEmitter = new FMODSoundEmitter();
        }

        worldSoundEmitter.fmodEmitter.x = x;
        worldSoundEmitter.fmodEmitter.y = y;
        worldSoundEmitter.fmodEmitter.z = (float)z;
        worldSoundEmitter.daytime = name;
        worldSoundEmitter.dawn = Rand.Next(7.0F, 8.0F);
        worldSoundEmitter.dusk = Rand.Next(19.0F, 20.0F);
        zombie.SoundManager.instance.registerEmitter(worldSoundEmitter.fmodEmitter);
        this.worldEmitters.add(worldSoundEmitter);
    }

    private void updatePowerSupply() {
        boolean _boolean = zombie.GameTime.getInstance().NightsSurvived < zombie.SandboxOptions.getInstance().getElecShutModifier();
        if (this.electricityShutOffState == -1) {
            IsoWorld.instance.setHydroPowerOn(_boolean);
        }

        if (this.electricityShutOffState == 0 && _boolean) {
            IsoWorld.instance.setHydroPowerOn(true);
            this.checkHaveElectricity();
        }

        if (this.electricityShutOffState == 1 && !_boolean) {
            if (this.electricityShutOffEmitter == null) {
                this.electricityShutOffEmitter = new FMODSoundEmitter();
            }

            if (!this.electricityShutOffEmitter.isPlaying(this.electricityShutOffEvent)) {
                Vector2f vector2f = new Vector2f();
                this.getListenerPos(vector2f);
                BuildingDef buildingDef = this.getNearestBuilding(vector2f.x, vector2f.y, vector2f);
                if (buildingDef == null) {
                    this.electricityShutOffEmitter.setPos(-1000.0F, -1000.0F, 0.0F);
                } else {
                    this.electricityShutOffEmitter.setPos(vector2f.x, vector2f.y, 0.0F);
                }

                this.electricityShutOffEvent = this.electricityShutOffEmitter.playSound("WorldEventElectricityShutdown");
                if (this.electricityShutOffEvent != 0L) {
                    fmod.javafmod.FMOD_Studio_EventInstance_SetCallback(
                        this.electricityShutOffEvent,
                        this.electricityShutOffEventCallback,
                        FMOD_STUDIO_EVENT_CALLBACK_TYPE.FMOD_STUDIO_EVENT_CALLBACK_TIMELINE_MARKER.bit
                    );
                }
            }
        }

        this.electricityShutOffState = _boolean ? 1 : 0;
        if (this.electricityShutOffEmitter != null) {
            this.electricityShutOffEmitter.tick();
        }
    }

    private void checkHaveElectricity() {
        for (int int0 = 0; int0 < IsoPlayer.numPlayers; int0++) {
            IsoChunkMap isoChunkMap = IsoWorld.instance.CurrentCell.ChunkMap[int0];
            if (!isoChunkMap.ignore) {
                for (int int1 = 0; int1 < 8; int1++) {
                    for (int int2 = isoChunkMap.getWorldYMinTiles(); int2 <= isoChunkMap.getWorldYMaxTiles(); int2++) {
                        for (int int3 = isoChunkMap.getWorldXMinTiles(); int3 <= isoChunkMap.getWorldXMaxTiles(); int3++) {
                            IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(int3, int2, int1);
                            if (isoGridSquare != null) {
                                for (int int4 = 0; int4 < isoGridSquare.getObjects().size(); int4++) {
                                    IsoObject isoObject = isoGridSquare.getObjects().get(int4);
                                    isoObject.checkHaveElectricity();
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public BuildingDef getNearestBuilding(float float0, float float1, Vector2f vector2f0) {
        IsoMetaGrid isoMetaGrid = IsoWorld.instance.getMetaGrid();
        int int0 = PZMath.fastfloor(float0 / 300.0F);
        int int1 = PZMath.fastfloor(float1 / 300.0F);
        BuildingDef buildingDef0 = null;
        float float2 = Float.MAX_VALUE;
        vector2f0.set(0.0F);
        Vector2f vector2f1 = new Vector2f();

        for (int int2 = int1 - 1; int2 <= int1 + 1; int2++) {
            for (int int3 = int0 - 1; int3 <= int0 + 1; int3++) {
                IsoMetaCell isoMetaCell = isoMetaGrid.getCellData(int3, int2);
                if (isoMetaCell != null && isoMetaCell.info != null) {
                    for (BuildingDef buildingDef1 : isoMetaCell.info.Buildings) {
                        float float3 = buildingDef1.getClosestPoint(float0, float1, vector2f1);
                        if (float3 < float2) {
                            float2 = float3;
                            buildingDef0 = buildingDef1;
                            vector2f0.set(vector2f1);
                        }
                    }
                }
            }
        }

        return buildingDef0;
    }

    private void getListenerPos(Vector2f vector2f) {
        IsoPlayer isoPlayer0 = null;
        vector2f.set(0.0F);

        for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
            IsoPlayer isoPlayer1 = IsoPlayer.players[_int];
            if (isoPlayer1 != null
                && (isoPlayer0 == null || isoPlayer0.isDead() && isoPlayer1.isAlive() || isoPlayer0.Traits.Deaf.isSet() && !isoPlayer1.Traits.Deaf.isSet())) {
                isoPlayer0 = isoPlayer1;
                vector2f.set(isoPlayer1.getX(), isoPlayer1.getY());
            }
        }
    }

    public static final class Ambient {
        public float x;
        public float y;
        public String name;
        float radius;
        float volume;
        int worldSoundRadius;
        int worldSoundVolume;
        public boolean trackMouse = false;
        final FMODSoundEmitter emitter = new FMODSoundEmitter();

        public Ambient(String _name, float _x, float _y, float _radius, float _volume) {
            this(_name, _x, _y, _radius, _volume, false);
        }

        public Ambient(String _name, float _x, float _y, float _radius, float _volume, boolean remote) {
            this.name = _name;
            this.x = _x;
            this.y = _y;
            this.radius = _radius;
            this.volume = _volume;
            this.emitter.x = _x;
            this.emitter.y = _y;
            this.emitter.z = 0.0F;
            this.emitter.playAmbientSound(_name);
            this.update();
            LuaEventManager.triggerEvent("OnAmbientSound", _name, _x, _y);
        }

        public boolean finished() {
            return this.emitter.isEmpty();
        }

        public void update() {
            this.emitter.tick();
            if (this.trackMouse && IsoPlayer.getInstance() != null) {
                float float0 = (float)Mouse.getXA();
                float float1 = (float)Mouse.getYA();
                float0 -= (float)IsoCamera.getScreenLeft(IsoPlayer.getPlayerIndex());
                float1 -= (float)IsoCamera.getScreenTop(IsoPlayer.getPlayerIndex());
                float0 *= Core.getInstance().getZoom(IsoPlayer.getPlayerIndex());
                float1 *= Core.getInstance().getZoom(IsoPlayer.getPlayerIndex());
                int _int = (int)IsoPlayer.getInstance().getZ();
                this.emitter.x = (float)((int)IsoUtils.XToIso(float0, float1, (float)_int));
                this.emitter.y = (float)((int)IsoUtils.YToIso(float0, float1, (float)_int));
            }

            if (!GameClient.bClient && this.worldSoundRadius > 0 && this.worldSoundVolume > 0) {
                zombie.WorldSoundManager.instance.addSound(null, (int)this.x, (int)this.y, 0, this.worldSoundRadius, this.worldSoundVolume);
            }
        }

        public void repeatWorldSounds(int _radius, int _volume) {
            this.worldSoundRadius = _radius;
            this.worldSoundVolume = _volume;
        }

        private IsoGameCharacter getClosestListener(float float4, float float5) {
            IsoPlayer isoPlayer0 = null;
            float float0 = Float.MAX_VALUE;

            for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
                IsoPlayer isoPlayer1 = IsoPlayer.players[_int];
                if (isoPlayer1 != null && isoPlayer1.getCurrentSquare() != null) {
                    float float1 = isoPlayer1.getX();
                    float float2 = isoPlayer1.getY();
                    float float3 = IsoUtils.DistanceToSquared(float1, float2, float4, float5);
                    if (isoPlayer1.Traits.HardOfHearing.isSet()) {
                        float3 *= 4.5F;
                    }

                    if (isoPlayer1.Traits.Deaf.isSet()) {
                        float3 = Float.MAX_VALUE;
                    }

                    if (float3 < float0) {
                        isoPlayer0 = isoPlayer1;
                        float0 = float3;
                    }
                }
            }

            return isoPlayer0;
        }
    }

    public static final class AmbientLoop {
        public static float volChangeAmount = 0.01F;
        public float targVol;
        public float currVol;
        public String name;
        public float volumedelta = 1.0F;
        public long channel = -1L;
        public final FMODSoundEmitter emitter = new FMODSoundEmitter();

        public AmbientLoop(float startVol, String _name, float volDel) {
            this.volumedelta = volDel;
            this.channel = this.emitter.playAmbientLoopedImpl(_name);
            this.targVol = startVol;
            this.currVol = 0.0F;
            this.update();
        }

        public void update() {
            if (this.targVol > this.currVol) {
                this.currVol = this.currVol + volChangeAmount;
                if (this.currVol > this.targVol) {
                    this.currVol = this.targVol;
                }
            }

            if (this.targVol < this.currVol) {
                this.currVol = this.currVol - volChangeAmount;
                if (this.currVol < this.targVol) {
                    this.currVol = this.targVol;
                }
            }

            this.emitter.setVolumeAll(this.currVol * this.volumedelta);
            this.emitter.tick();
        }

        public void stop() {
            this.emitter.stopAll();
        }
    }

    public static final class WorldSoundEmitter {
        public FMODSoundEmitter fmodEmitter;
        public float x;
        public float y;
        public float z;
        public long channel = -1L;
        public String daytime;
        public float dawn;
        public float dusk;
    }
}
